import Vue from 'vue'
import VueRouter from 'vue-router'
import BookManage from '../views/BookManage'
import AddBook from '../views/AddBook'
import Index from '../views/Index'
import BookUpdate from '../views/BookUpdate'
import UserManage from '../views/UserManage'
import UserIndex from '../views/UserIndex'
import UserUpdate from '../views/UserUpdate'
import AddUser from '../views/AddUser'
import Login from '../views/Login'

Vue.use(VueRouter)

const routes = [
  {
    path:"/login",
    name:"登录",
    component:Login,
    show:false
  },
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/user/home',
    name: '用户首页',
    component: () => import('../views/UserHome.vue'),
    show: false
  },
  {
    path:"/admin",
    name:"图书管理",
    component:Index,
    show:true,
    redirect:"/admin/BookManage",
    children:[
      {
        path:"BookManage",
        name:"查询图书",
        component:BookManage
      },
      {
        path:"AddBook",
        name:"添加图书",
        component:AddBook
      }
    ]
  },
  {
    path:"/admin/user",
    name:"用户管理",
    component:UserIndex,
    show:true,
    redirect:"/admin/user/UserManage",
    children:[
      {
        path:"UserManage",
        name:"用户列表",
        component:UserManage
      },
      {
        path:"AddUser",
        name:"添加用户",
        component:AddUser
      }
    ]
  },
  {
    path:"/admin/category",
    name:"分类管理",
    component:UserIndex,
    show:true,
    redirect:"/admin/category/BookCategoryManage",
    children:[
      {
        path:"BookCategoryManage",
        name:"分类列表",
        component:() => import('../views/BookCategoryManage.vue')
      },
      {
        path:"AddBookCategory",
        name:"添加分类",
        component:() => import('../views/AddBookCategory.vue')
      }
    ]
  },
  {
    path:"/admin/borrow",
    name:"借阅管理",
    component:UserIndex,
    show:true,
    redirect:"/admin/borrow/BorrowManage",
    children:[
      {
        path:"BorrowManage",
        name:"借阅记录",
        component:() => import('../views/BorrowManage.vue')
      }
    ]
  },
  {
    path:'/admin/update',
    name:'编辑图书',
    component:BookUpdate,
    show:false
  },
  {
    path:'/admin/category/update',
    component:() => import('../views/BookCategoryUpdate.vue'),
    show:false
  },
  {
    path:'/admin/user/update',
    component:UserUpdate,
    show:false
  },
  {
    path:'/userUpdate',
    component:UserUpdate,
    show:false
  }
]

// 模拟获取当前用户角色
function getCurrentUserRole() {
  return localStorage.getItem('userRole') || 'user'
}

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  const userRole = getCurrentUserRole()
  
  // 严格检查未登录状态
  const isAuthenticated = !!userRole
  const isLoginPage = to.path === '/login'
  const isRootRedirect = to.path === '/'
  
  if (!isAuthenticated && !isLoginPage && !isRootRedirect) {
    next('/login')
    return
  }
  
  // 定义管理员专属路由
  const adminOnlyRoutes = [
    '/admin',
    '/admin/user/UserManage',
    '/admin/user/AddUser',
    '/user/UserManage'
  ]
  
  // 检查管理员权限
  if (isAuthenticated && 
      (to.path.startsWith('/admin') || adminOnlyRoutes.includes(to.path)) && 
      userRole !== 'admin') {
    next('/user/home')
  } else {
    next()
  }
})

export default router
